

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/taotaogitee.gitee.io/img/favicon.png">
  <link rel="icon" href="/taotaogitee.gitee.io/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#447EA5">
  <meta name="author" content="Oscar">
  <meta name="keywords" content="">
  
    <meta name="description" content="前言为什么要实现动态数据源呢？这次罗涛涛遇到了一个需要根据不同的业务单跟换数据源（也就是数据库地址），简单的比喻就是：管理员上传一套关于数据库操作的试卷，学生要做作业就需要操作数据库，那么用本地数据库就达不到效果了。而且就算利用多数据源的原理，我提前在后端写死也不符合设计模型原理，动态数据源就是为了解决这个场景出现的，可以说是非常契合的解决方案。 原理Spring提供了AbstractRoutin">
<meta property="og:type" content="article">
<meta property="og:title" content="实现动态数据源">
<meta property="og:url" content="http://taotaogitee.gitee.io/taotaogitee.gitee.io/2022/07/06/%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E6%BA%90/index.html">
<meta property="og:site_name" content="罗涛涛のBlog">
<meta property="og:description" content="前言为什么要实现动态数据源呢？这次罗涛涛遇到了一个需要根据不同的业务单跟换数据源（也就是数据库地址），简单的比喻就是：管理员上传一套关于数据库操作的试卷，学生要做作业就需要操作数据库，那么用本地数据库就达不到效果了。而且就算利用多数据源的原理，我提前在后端写死也不符合设计模型原理，动态数据源就是为了解决这个场景出现的，可以说是非常契合的解决方案。 原理Spring提供了AbstractRoutin">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://taotaogitee.gitee.io/taotaogitee.gitee.io/img/blog/6.png">
<meta property="article:published_time" content="2022-07-06T14:44:38.503Z">
<meta property="article:modified_time" content="2022-07-06T15:12:55.595Z">
<meta property="article:author" content="Oscar">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://taotaogitee.gitee.io/taotaogitee.gitee.io/img/blog/6.png">
  
  
  <title>实现动态数据源 - 罗涛涛のBlog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/taotaogitee.gitee.io/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"taotaogitee.gitee.io","root":"/taotaogitee.gitee.io/","version":"1.8.14","typing":{"enable":true,"typeSpeed":80,"cursorChar":"|","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/taotaogitee.gitee.io/local-search.xml"};
  </script>
  <script  src="/taotaogitee.gitee.io/js/utils.js" ></script>
  <script  src="/taotaogitee.gitee.io/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/taotaogitee.gitee.io/">
      <strong>罗涛涛の博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/taotaogitee.gitee.io/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/taotaogitee.gitee.io/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/taotaogitee.gitee.io/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/taotaogitee.gitee.io/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/taotaogitee.gitee.io/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/taotaogitee.gitee.io/img/bg.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="实现动态数据源">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-07-06 22:44" pubdate>
        2022年7月6日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      41 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">实现动态数据源</h1>
            
            <div class="markdown-body">
              <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>为什么要实现动态数据源呢？这次罗涛涛遇到了一个需要根据不同的业务单跟换数据源（也就是数据库地址），简单的比喻就是：管理员上传一套关于数据库操作的试卷，学生要做作业就需要操作数据库，那么用本地数据库就达不到效果了。而且就算利用多数据源的原理，我提前在后端写死也不符合设计模型原理，动态数据源就是为了解决这个场景出现的，可以说是非常契合的解决方案。</p>
<h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><p>Spring提供了AbstractRoutingDataSource用于动态路由数据源，第一种场景继承AbstractRoutingDataSource类并覆写其protected abstract Object determineCurrentLookupKey()即可;<br>而第二种场景我们直接覆写protected DataSource determineTargetDataSource方法即可。原理可看下AbstractRoutingDataSource对应源码，比较简单，不做赘述。</p>
<h3 id="演示"><a href="#演示" class="headerlink" title="演示"></a>演示</h3><figure class="highlight java"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></div></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.yaoshun.util.spring.SpringUtils;<br><span class="hljs-keyword">import</span> org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;<br><span class="hljs-keyword">import</span> java.util.Map;<br><span class="hljs-keyword">import</span> java.util.concurrent.ConcurrentHashMap;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * &lt;p&gt;使用步骤：&lt;/p&gt;</span><br><span class="hljs-comment"> * &lt;blockquote&gt;&lt;pre&gt;</span><br><span class="hljs-comment"> *     DynamicDataSource.dataSourcesMap.put(dataSourceKey, druidDataSource);</span><br><span class="hljs-comment"> *     DynamicDataSource.setDataSource(dataSourceKey);</span><br><span class="hljs-comment"> *     调用业务代码&lt;/i&gt;</span><br><span class="hljs-comment"> *     DynamicDataSource.clear();</span><br><span class="hljs-comment"> * &lt;/pre&gt;&lt;/blockquote&gt;</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DynamicDataSource</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractRoutingDataSource</span> &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> ThreadLocal&lt;String&gt; dataSourceKey = ThreadLocal.withInitial(() -&gt; <span class="hljs-string">&quot;defaultDataSource&quot;</span>);<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map&lt;Object, Object&gt; dataSourcesMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ConcurrentHashMap</span>&lt;&gt;(<span class="hljs-number">10</span>);<br><br>    <span class="hljs-keyword">static</span> &#123;<br>        dataSourcesMap.put(<span class="hljs-string">&quot;defaultDataSource&quot;</span>, SpringUtils.getBean(<span class="hljs-string">&quot;defaultDataSource&quot;</span>));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> Object <span class="hljs-title function_">determineCurrentLookupKey</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> DynamicDataSource.dataSourceKey.get();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setDataSource</span><span class="hljs-params">(String dataSource)</span> &#123;<br>        DynamicDataSource.dataSourceKey.set(dataSource);<br>        <span class="hljs-type">DynamicDataSource</span> <span class="hljs-variable">dynamicDataSource</span> <span class="hljs-operator">=</span> (DynamicDataSource) SpringUtils.getBean(<span class="hljs-string">&quot;dataSource&quot;</span>);<br>        dynamicDataSource.afterPropertiesSet();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">getDataSource</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> DynamicDataSource.dataSourceKey.get();<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">clear</span><span class="hljs-params">()</span> &#123;<br>        DynamicDataSource.dataSourceKey.remove();<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;<br><span class="hljs-keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.DependsOn;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Primary;<br><span class="hljs-keyword">import</span> javax.sql.DataSource;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DataSourceConfig</span> &#123;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-meta">@ConfigurationProperties(&quot;spring.datasource.druid&quot;)</span><br>    <span class="hljs-keyword">public</span> DataSource <span class="hljs-title function_">defaultDataSource</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> DruidDataSourceBuilder.create().build();<br>    &#125;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-meta">@Primary</span><br>    <span class="hljs-meta">@DependsOn(&#123;&quot;springUtils&quot;, &quot;defaultDataSource&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> DynamicDataSource <span class="hljs-title function_">dataSource</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">DynamicDataSource</span> <span class="hljs-variable">dynamicDataSource</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DynamicDataSource</span>();<br>        dynamicDataSource.setTargetDataSources(DynamicDataSource.dataSourcesMap);<br>        <span class="hljs-keyword">return</span> dynamicDataSource;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h4 id="使用时直接调用DynamicDataSource-setDataSource-DataSource-dataSource-方法即可，使用完后调用DynamicDataSource-clear-防止内存泄漏并重置默认数据源。"><a href="#使用时直接调用DynamicDataSource-setDataSource-DataSource-dataSource-方法即可，使用完后调用DynamicDataSource-clear-防止内存泄漏并重置默认数据源。" class="headerlink" title="使用时直接调用DynamicDataSource.setDataSource(DataSource dataSource)方法即可，使用完后调用DynamicDataSource.clear()防止内存泄漏并重置默认数据源。"></a>使用时直接调用DynamicDataSource.setDataSource(DataSource dataSource)方法即可，使用完后调用DynamicDataSource.clear()防止内存泄漏并重置默认数据源。</h4><h4 id="附上详细使用方法："><a href="#附上详细使用方法：" class="headerlink" title="附上详细使用方法："></a>附上详细使用方法：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//        更换数据源</span><br>        <span class="hljs-type">DruidDataSource</span> <span class="hljs-variable">druidDataSource</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DruidDataSource</span>();<br>        druidDataSource.setUrl(ip);<span class="hljs-comment">//ip指的是数据源的IP地址</span><br>        druidDataSource.setUsername(username);<span class="hljs-comment">//username：数据库的用户名</span><br>        druidDataSource.setPassword(password);<span class="hljs-comment">//password：数据库密码，值得一提的是，这里默认是3306的端口</span><br><br>        <span class="hljs-comment">//DruidDataSource 2487行</span><br>        druidDataSource.setBreakAfterAcquireFailure(<span class="hljs-literal">true</span>);<br>        <span class="hljs-comment">//前端快速响应</span><br>        druidDataSource.setFailFast(<span class="hljs-literal">true</span>);<br>        druidDataSource.setMaxWait(<span class="hljs-number">10000</span>);<br>        druidDataSource.setValidationQuery(<span class="hljs-string">&quot;select 1 from dual&quot;</span>);<br>        druidDataSource.setValidationQueryTimeout(<span class="hljs-number">5000</span>);<br>        DynamicDataSource.dataSourcesMap.put(<span class="hljs-string">&quot;dbkey&quot;</span>, druidDataSource);<br>        DynamicDataSource.setDataSource(<span class="hljs-string">&quot;dbkey&quot;</span>);<br><span class="hljs-comment">//        业务代码</span><br>		<span class="hljs-comment">//此处写自己的业务代码，我这里已经拿掉业务代码</span><br><br><span class="hljs-comment">//        恢复默认数据源</span><br>        DynamicDataSource.clear();<br></code></pre></td></tr></table></figure>

<h5 id="此处再创建一个工具类SpringUtils，这是动态数据源需要使用到的"><a href="#此处再创建一个工具类SpringUtils，这是动态数据源需要使用到的" class="headerlink" title="此处再创建一个工具类SpringUtils，这是动态数据源需要使用到的"></a>此处再创建一个工具类SpringUtils，这是动态数据源需要使用到的</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> lombok.Getter;<br><span class="hljs-keyword">import</span> org.springframework.beans.BeansException;<br><span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;<br><span class="hljs-keyword">import</span> org.springframework.context.ApplicationContextAware;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SpringUtils</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ApplicationContextAware</span> &#123;<br><br>    <span class="hljs-meta">@Getter</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ApplicationContext applicationContext;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setApplicationContext</span><span class="hljs-params">(ApplicationContext applicationContext)</span> <span class="hljs-keyword">throws</span> BeansException &#123;<br>        <span class="hljs-keyword">if</span> (SpringUtils.applicationContext == <span class="hljs-literal">null</span>) &#123;<br>            SpringUtils.applicationContext = applicationContext;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; T <span class="hljs-title function_">getBean</span><span class="hljs-params">(Class&lt;T&gt; clazz)</span> &#123;<br>        <span class="hljs-keyword">return</span> SpringUtils.applicationContext.getBean(clazz);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Object <span class="hljs-title function_">getBean</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-keyword">return</span> SpringUtils.applicationContext.getBean(name);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title function_">getProperty</span><span class="hljs-params">(String key)</span> &#123;<br>        <span class="hljs-keyword">return</span> SpringUtils.applicationContext.getEnvironment().getProperty(key);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h3><p>到这里动态数据源的分享就结束啦。总的来说，动态数据源是利用Spring提供的AbstractRoutingDataSource，为了了解其中原理还需要去看看源码。</p>
<h3 id="分享"><a href="#分享" class="headerlink" title="分享"></a>分享</h3><p>马上要去实习了，前前后后也投了很多简历，也去企业面试过，很遗憾没有一次就拿到offer。也不知道是命运还是庆幸，没有拿到offer让我不得不参加学校安排得到小学期学习，是一个类似培训机构的速成培训过程，从基础到高深，我想也许留下来学习说不定更有价值，少赚一个月的钱，让我可以多沉淀沉淀。最近总会想，说不定实习结束后还真的要去北上广深发展，福建确实没有那么多机会（真的不推荐来计算机专业，连就业都难谈何高新）。加油吧！</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/taotaogitee.gitee.io/categories/%E6%8A%80%E6%9C%AF/">技术</a>
                    
                      <a class="hover-with-bg" href="/taotaogitee.gitee.io/categories/%E6%8A%80%E6%9C%AF/%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E6%BA%90/">动态数据源</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/taotaogitee.gitee.io/tags/MySQL/">MySQL</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/taotaogitee.gitee.io/2022/05/15/%E6%B5%85%E8%B0%88Tomcat%E5%8F%91%E5%B8%83/">
                        <span class="hidden-mobile">浅谈Tomcat发布</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>游戏</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>人生</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/taotaogitee.gitee.io/js/events.js" ></script>
<script  src="/taotaogitee.gitee.io/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/taotaogitee.gitee.io/js/local-search.js" ></script>



  
    <script  src="/taotaogitee.gitee.io/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/taotaogitee.gitee.io/js/boot.js" ></script>


</body>
</html>
